package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strings"
)

func main() {
	// 打开连接
	conn, err := net.Dial("tcp", "localhost:50000")
	if err != nil {
		fmt.Println("Error dial:", err)
		return
	}

	// 读取输入
	reader := bufio.NewReader(os.Stdin)
	fmt.Println("请输入昵称:")
	name, err := reader.ReadString('\n')
	if err != nil {
		fmt.Println("Error reading:", err)
		return
	}
	name = strings.TrimRight(name, "\n")
	for {
		fmt.Println("请输入发送到服务器的消息, 输入'quit'或'exit'退出")
		input, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("Error reading:", err)
			return
		}
		input = strings.TrimRight(input, "\n")
		if input == "quit" || input == "exit" {
			fmt.Println("App quit.")
			return
		}
		conn.Write([]byte(name + " says: " + input))
	}
}
